﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using Acms.WebControls;
using Acms.Bll;

public partial class Topics_Default : BaseTopicPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //System.Threading.Thread.Sleep(1000);
    }

    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
    }
    

    public override Acms.Bll.Role.Roles[] AllowedRoles
    {
        get { return new Acms.Bll.Role.Roles[] { Acms.Bll.Role.Roles.Developer, Acms.Bll.Role.Roles.ContentEditor, Acms.Bll.Role.Roles.ContentViewer, Acms.Bll.Role.Roles.ContentPublisher }; }
    }
        
    protected void dsGroups_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.TopicGroup> e)
    {
        e.SetData(Acms.Bll.TopicGroup.GetAllBySiteId(AcmsContext.SiteId));
    }

    protected void dlGroups_ItemDataBound(object sender, Acms.WebControls.AcmsDataListItemEventArgs e)
    {
        if (e.Item.IsDataItem)
        {
            TopicGroup group = e.Item.DataItem as TopicGroup;
            AjaxControlToolkit.Accordion itemAccordian = e.Item.FindControl("itemAccordian") as AjaxControlToolkit.Accordion;
            if (itemAccordian != null)
            {
                itemAccordian.DataSource = new TopicGroup[] { group };
                itemAccordian.DataBind();
            }
            RadGrid itemGrid = e.Item.FindControl("itemGrid") as RadGrid;
            if (itemGrid != null)
            {
                itemGrid.DataSource = group.Topics;
                itemGrid.DataBind();
            }
        }
    }

    protected void itemGrid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        RadGrid grid = sender as RadGrid;
        if (grid != null)
        {
            AcmsDataListItem group = grid.NamingContainer as AcmsDataListItem;
            if (group != null && dlGroups.DataKeys != null)
            {
                int id = (int)dlGroups.DataKeys[group.ItemIndex]["Id"];
                grid.DataSource = Topic.GetAllByTopicGroupId(id);
            }
        }
    }

    protected void itemGrid_InsertCommand(object sender, GridCommandEventArgs e)
    {
        RadGrid grid = sender as RadGrid;
        GridDataInsertItem item = e.Item as GridDataInsertItem;
        if (grid != null && item != null)
        {
            AcmsDataListItem group = grid.NamingContainer as AcmsDataListItem;
            if (group != null && dlGroups.DataKeys != null)
            {
                int groupId = (int)dlGroups.DataKeys[group.ItemIndex]["Id"];
                Topic topic = Topic.CreateInstance();
                topic.TopicGroupId = groupId;
                item.UpdateValues(topic);
                topic.Update();
            }
        }
    }

    protected void itemGrid_UpdateCommand(object sender, GridCommandEventArgs e)
    {
        RadGrid grid = sender as RadGrid;
        GridEditableItem item = e.Item as GridEditableItem;
        if (grid != null && item != null)
        {

            int id = (int)item.GetDataKeyValue("Id");
            object concurencyId = (byte[])item.GetDataKeyValue("ConcurencyId");
            Topic topic = Topic.GetById(id);
            if (topic != null)
            {
                item.UpdateValues(topic);
                topic.Update();
            }
            else
            {
                if (topic != null)
                {
                    throw new Acms.Exceptions.ConcurencyException();
                }
                else
                {
                    throw new Acms.Exceptions.IdNotFoundException();
                }
            }
        }
    }

    protected void itemGrid_DeleteCommand(object sender, GridCommandEventArgs e)
    {
        RadGrid grid = sender as RadGrid;
        if (grid != null)
        {
            int id = (int)grid.MasterTableView.DataKeyValues[e.Item.ItemIndex]["Id"];
            object concurencyId = grid.MasterTableView.DataKeyValues[e.Item.ItemIndex]["ConcurencyId"];

            Topic topic = Topic.GetById(id);
            if (topic != null)
            {
                topic.Delete();
                grid.Rebind();
            }
            else
            {
                if (topic != null)
                {
                    throw new Acms.Exceptions.ConcurencyException();
                }
                else
                {
                    throw new Acms.Exceptions.IdNotFoundException();
                }
            }
        }
    }

    protected void dsTopics_SelectData(object sender, Acms.DataControls.SelectEventArgs<Topic> e)
    {
        e.SetData(Topic.GetAllBySiteId(AcmsContext.SiteId));

    }

    protected void dlGroups_ItemCreated(object sender, AcmsDataListItemEventArgs e)
    {
        if (e.Item.IsDataItem)
        {
            RadGrid itemGrid = e.Item.FindControl("itemGrid") as RadGrid;
            RadAjaxLoadingPanel loading = e.Item.FindControl("loading") as RadAjaxLoadingPanel;
            if (itemGrid != null && loading != null)
            {
                ajaxManager.AjaxSettings.AddAjaxSetting(itemGrid, itemGrid, loading);
            }
        }
    }

    protected void itemGrid_ItemCommand(object sender, GridCommandEventArgs e)
    {
        if (e.CommandName == "MoveUp" || e.CommandName == "MoveDown")
        {
            RadGrid grid = sender as RadGrid;
            if (grid != null)
            {
                AcmsDataListItem group = grid.NamingContainer as AcmsDataListItem;
                if (group != null && dlGroups.DataKeys != null)
                {
                    int newSelectedIndex = -1;
                    int id = (int)dlGroups.DataKeys[group.ItemIndex]["Id"];
                    List<Topic> topics =Topic.GetAllByTopicGroupId(id);
                    //get the moving
                    Topic moving = topics[e.Item.DataSetIndex];
                    //get the move to
                    Topic replacing = null;
                    if (e.CommandName == "MoveUp")
                    {
                        newSelectedIndex = e.Item.ItemIndex - 1;
                        replacing = topics[e.Item.DataSetIndex - 1];
                    }
                    else
                    {
                        newSelectedIndex = e.Item.ItemIndex + 1;
                        replacing = topics[e.Item.DataSetIndex + 1];
                    }

                    bool saveAll = false;
                    if (moving.Sequence == replacing.Sequence)
                    {
                        saveAll = true;
                        for (int i = 0; i < topics.Count; i++)
                        {
                            if (topics[i].Sequence != i)
                            {
                                topics[i].Sequence = i;
                            }
                        }
                    }

                    int tmpSequence = moving.Sequence;
                    moving.Sequence = replacing.Sequence;
                    replacing.Sequence = tmpSequence;

                    if (saveAll)
                    {
                        topics[0].BeginTransaction();
                        foreach (Topic t in topics)
                        {
                            if (t != topics[0])
                            {
                                t.UseTransaction(topics[0].TransactionKey);
                            }
                            t.Update();
                        }
                        topics[0].CommitTransaction();
                    }
                    else
                    {
                        moving.BeginTransaction();
                        replacing.UseTransaction(moving.TransactionKey);
                        moving.Update();
                        replacing.Update();
                        moving.CommitTransaction();
                    }

                    if (grid.AllowPaging)
                    {
                        if (e.CommandName == "MoveUp" && e.Item.ItemIndex == 0)
                        {
                            grid.CurrentPageIndex--;
                            newSelectedIndex = grid.PageSize - 1;
                        }
                        else if (e.CommandName == "MoveDown" && e.Item.ItemIndex + 1 == grid.PageSize)
                        {
                            grid.CurrentPageIndex++;
                            newSelectedIndex = 0;
                        }
                    }

                    grid.SelectedIndexes.Clear();
                    grid.Rebind();
                    grid.MasterTableView.Items[newSelectedIndex].Selected = true;
                }
            }
        }
    }

    protected void itemGrid_ItemDataBound(object sender, GridItemEventArgs e)
    {
    }
}
